package consts

const (
	MySwaggerUITemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<meta name="description" content="SwaggerUI"/>
	<title>SwaggerUI</title>
	<!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui.min.css" />-->
	<!--<link rel="stylesheet" href="https://cdn.staticfile.org/swagger-ui/4.15.5/swagger-ui.min.css" />-->
	<link rel="stylesheet" href="/assets/swagger/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui-bundle.js" crossorigin></script>-->
<!--<script src="https://cdn.staticfile.org/swagger-ui/4.15.5/swagger-ui-bundle.min.js" crossorigin></script>-->
<script src="/assets/swagger/swagger-ui-bundle.js" crossorigin></script>
<script>
	window.onload = function() {
	  fetch('./swagger/api.json')
	  .then(function(response) {
		response.json()
		.then(function(json) {
		  // json.schemes[0] = window.location.protocol.slice(0, -1)
		  json.host = window.location.host
	
		  const ui = SwaggerUIBundle({
			spec: json,
			dom_id: '#swagger-ui',
			deepLinking: true,
			presets: [
			  SwaggerUIBundle.presets.apis
			],
			plugins: [
			  SwaggerUIBundle.plugins.DownloadUrl
			],
			requestInterceptor: function(req) {
				var accessToken = localStorage.getItem('accessToken')
				if (accessToken.indexOf('Bearer ') != 0) {
					accessToken = 'Bearer ' + accessToken
				}

			  req.headers.Authorization = accessToken
			  return req
			}
		  })
	
		  window.ui = ui
		  
   		  setTimeout(function () {
			addAuthButton();
          }, 1000); // 延迟1秒
		})
	  })
	}
	  
	function addAuthButton() {

	  var div = document.createElement('div')
	  var button = document.createElement('button')
	  button.appendChild(document.createTextNode('Authorization'))
	  button.style = 'position: absolute; right: 300px;font-size: 14px; font-weight: 700; min-width: 80px; padding: 6px 15px; text-align: center; border-radius: 3px; background: #41cd8e; text-shadow: 0 1px 0 rgba(0,0,0,.1); font-family: Titillium Web,sans-serif; color: #fff; border: none;'
	  button.addEventListener('click', function(event) {
		event.preventDefault()
		var token = prompt('Enter JWT access token token. Eg. eyJraW...qJSaxQ. Note: do not need to put Bearer in the beginning')
		if (!token) {
		  // localStorage.removeItem('accessToken')
		  // alert('Token cleared')
		} else {
			var accessToken = token
			var bearer = token.indexOf('Bearer ') == 0
			if (bearer) {
				token = token.substring(7)
			}

		    token = token.replace(/[^\d\w_.-]/gi, '')

			if (bearer) {
				accessToken = 'Bearer ' + token
			}

		  localStorage.setItem('accessToken', accessToken)
		}
	  })
	
	  var container = document.querySelector('.information-container')
	  container.appendChild(div.appendChild(button))
	}
</script>
</body>
</html>
`
)
